package org.budo.jedis.pool;

import org.budo.graph.metric.RedisMetric;
import org.budo.jedis.pool.util.BudoJedisPoolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * @author lmw
 */
public class BudoJedisPool extends AbstractBudoJedisPool implements RedisMetric {
    private static final Logger log = LoggerFactory.getLogger(BudoJedisPool.class);

    public BudoJedisPool(JedisPoolConfig jedisPoolConfig, String host, int port, int timeout, String password, int database) {
        super(jedisPoolConfig, host, port, timeout, password, database);
    }

    public BudoJedisPool(String host, String password) {
        this(BudoJedisPoolUtil.defaultJedisPoolConfig(), host, Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, password, Protocol.DEFAULT_DATABASE);
    }

    @Override
    public Integer getRedisNumActive() {
        return this.getNumActive();
    }

    @Override
    public Integer getRedisMaxTotal() {
        return this.getJedisPoolConfig().getMaxTotal();
    }

    @Override
    public Jedis getResource() {
        try {
            return super.getResource();
        } catch (Throwable e) {
            String message = "#25 BudoJedisPool#getResource error, e=" + e + ", numActive=" + this.getRedisNumActive() //
                    + ", redisMaxTotal=" + this.getRedisMaxTotal() + ", this=" + this;

            log.error(message, e);
            throw new JedisConnectionException(message, e);
        }
    }
}
